/****************************
 * @author Laureta Xhaferaj
 * @email laureta@first.al
 * 
 * form-ajax validation
 */
jQuery(document).ready(function()
{
    jQuery('#form-validate').submit(function()
    {
        var url = $(this).attr("action");
        jQuery.ajax({
            url: url,
            type: "post",
            data: jQuery('form').serialize(),
            datatype: "json",
            beforeSend: function()
            {
                jQuery('#ajax-loading').show();
                jQuery(".validation-error-inline").hide();
            }
        })
                .done(function(data)
                {
                    $('#validation-div').empty()
                    if (data.validation_failed === 1)
                    {
                        var arr = data.errors;
                        jQuery.each(arr, function(index, value)
                        {
                            if (value.length !== 0)
                            {
                                $("#validation-div").addClass('alert alert-danger');
                                document.getElementById("validation-div").innerHTML += '<span class="glyphicon glyphicon-warning-sign"></span>' + ' ' + value + '<br/>';
                            }
                        });
                        jQuery('#ajax-loading').hide();
                    }
                    else {
                        window.location = data.redirect_to;
                    }
                })
                .fail(function(jqXHR, ajaxOptions, thrownError)
                {
                    alert('Invalid Data');
                });
        return false;
    });
});

